<?php

namespace App\Models\OperationManagement;

use Grimzy\LaravelMysqlSpatial\Types\Point;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;

class TrashSite extends Model
{
    use SoftDeletes;
    use SpatialTrait;
    protected $guarded = [];
    protected $spatialFields = ['location'];

    public function getList($request)
    {
    	$per_page = $request->input('per_page',10);
    	return self::orderBy('id','desc')
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含name、type参数
                    $name = $request->input('name', '');//垃圾点位
                    $type = $request->input('type','');//垃圾类型
                    $location = $request->input('location','');
                    if(!empty($name)) {
                        $query->where('name','like','%'.$name.'%');
                    }
                    if(!empty($type)) {
                        $query->where('type','=',$type);
                    }
                    if(!empty($location)) {
                        $location = Point::fromString($location);
                        $query->distance('location', $location, 10);
                    }
                })
                ->paginate($per_page)->toArray();
    }
}
